package com.example.itshome.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.itshome.R;
import com.example.itshome.adapter.MainPageFragmentAdapter;
import com.example.itshome.fragment.ActivityItem1Fragment;
import com.example.itshome.fragment.ActivityItem2Fragment;

import java.util.ArrayList;
import java.util.List;

public class ActivityListPageActivity extends AppCompatActivity implements View.OnClickListener {


    private TextView circleTextView;
    private TextView activityTextView;
    private ImageView backImageView;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_list_page);
        initFind();
        initButton();
        initFragment();
    }

    void initFind(){
        activityTextView=findViewById(R.id.txv_activity_page_activity);
        circleTextView=findViewById(R.id.txv_activity_page_circle);
        mViewPager=findViewById(R.id.vp_activity_page);
        backImageView=findViewById(R.id.imv_activity_page_back);
        activityTextView.setTag(1);
        circleTextView.setTag(2);
    }

    void initFragment(){
        List<Fragment> fragments=new ArrayList<>();
        fragments.add(new ActivityItem1Fragment());
        fragments.add(new ActivityItem2Fragment());
        mViewPager.setAdapter(new MainPageFragmentAdapter(getSupportFragmentManager(),fragments));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                pageChangeTo(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        pageChangeTo(0);
    }

    void initButton(){
        circleTextView.setOnClickListener(this);
        activityTextView.setOnClickListener(this);
        backImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    private void pageChangeTo(int position){
        switch (position){
            case 0:activityTextView.setSelected(true);circleTextView.setSelected(false);break;
            case 1:activityTextView.setSelected(false);circleTextView.setSelected(true); break;

        }

    }


    @Override
    public void onClick(View view) {
        int vid=(int)view.getTag();
        switch (vid){
            case 1:pageChangeTo(0);mViewPager.setCurrentItem(0);;break;
            case 2:pageChangeTo(1);mViewPager.setCurrentItem(1);;break;
        }
    }
}